package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        log.info(dishDto.toString());
        //这里逻辑较多，写道service中去
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }

    /**
     * 菜品信息的分页
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(int page, int pageSize, String name) {
        //1:添加分页构造器
        Page<Dish> pageInfo = new Page(page, pageSize);
        //2:添加条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //3:添加过滤条件
        queryWrapper.like(name != null, Dish::getName, name);
        //4:添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //5:执行分页查询
        dishService.page(pageInfo, queryWrapper);
        //因为页面上显示的是分类不是id
        Page<DishDto> dishDtoPage = new Page<>();
        //完成对象的转换
        BeanUtils.copyProperties(pageInfo, dishDtoPage, "records");
        List<DishDto> list = new ArrayList<>();
        for (Dish record : pageInfo.getRecords()) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(record, dishDto);
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                dishDto.setCategoryName(category.getName());
            }

            list.add(dishDto);
        }
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);

    }

    /**
     * 根据id查询菜品信息和口味信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id) {
        log.info(id.toString());
        R<DishDto> dishDtoR = dishService.selectDishwithFlavor(id);
        return dishDtoR;
    }

    /**
     * 按提交内容更新菜品信息或者口味信息
     *
     * @param dto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dto) {
        log.info(dto.toString());
        return dishService.updateDishWithFlavor(dto);
    }

    /**
     * 根据id删除菜品信息   我把删除单个和批量删除放在一起了   起售禁售一样的操作
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long... ids) {
        log.info("id={}", ids);
        List<Long> list = new ArrayList<>();
        for (Long id : ids) {
            list.add(id);
        }
        dishService.removeByIds(list);
        return R.success("删除成功");
    }

    @PostMapping("/status/0")
    public R<String> updateStatusout(Long ids) {
        log.info("id={}", ids);
        //1:创建构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getId, ids);
        //2:修改菜品信息中的起售字段为0
        Dish dish = new Dish();
        dish.setStatus(0);
        dishService.update(dish, queryWrapper);
        return R.success("成功停售");
    }

    @PostMapping("/status/1")
    public R<String> updateStatusbegin(Long ids) {
        log.info("id={}", ids);
        //1:创建构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getId, ids);
        //2:修改菜品信息中的起售字段为0
        Dish dish = new Dish();
        dish.setStatus(1);
        dishService.update(dish, queryWrapper);
        return R.success("成功起售");
    }

    /**
     * 根据分类id
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> selectbyCategoryId(Long categoryId,String name) {
        log.info("id={}", categoryId);
        //通过传进来的categoryid查菜品的集合
        //1:创建条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //2:设置条件
        queryWrapper.eq(categoryId!=null,Dish::getCategoryId, categoryId);
        //只查找那些状态等于1的  1代表起售，0代表禁售
        queryWrapper.eq(Dish::getStatus,1);
        //3:设置过滤条件
        queryWrapper.like(name!=null,Dish::getName,name);
        //4；设置排序条件
        queryWrapper.orderByAsc(Dish::getPrice);
        //3:查询
        List<Dish> list = dishService.list(queryWrapper);
        //提前准备一个集合
        List<DishDto> dishDtos = new ArrayList<>();
        //前台访问的也是这个地址，但是口味数据没有被封装进去，这个使用uto对象
        for (Dish dish : list) {
            //先获取当前菜品的id，然后通过dishfalour中的dish_id查口味数据
            Long id = dish.getId();
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            //disdto里面应当封装口味数据  这里将口味数据封装进去
            LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DishFlavor::getDishId,id);
            List<DishFlavor> list1 = dishFlavorService.list(queryWrapper1);
            dishDto.setFlavors(list1);
            dishDtos.add(dishDto);
        }
        return R.success(dishDtos);
    }
}
